package cn.tutu.blockchain.controller;

import java.util.List;

import org.hibernate.validator.constraints.NotBlank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.tutu.blockchain.common.MsgObj;
import cn.tutu.blockchain.fileInfo.service.FileInfoService;

@RestController
@RequestMapping("/machineInfo")
@Validated
public class FileInfoController {

	private static Logger LOGGER = LoggerFactory.getLogger(SysuserController.class);

	@Autowired
	private FileInfoService fileInfoService;

	@RequestMapping("/create")
	public MsgObj create(@NotBlank(message = "矿机ID不能为空!") String minemachineId,
			@NotBlank(message = "文件hash不能为空!") String fileHash, @NotBlank(message = "hash类型不能为空") String type) {
		if ("QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn".equals(fileHash)) {
			LOGGER.info("忽略文件信息:矿机ID{},文件hash{}", minemachineId, fileHash);
			return MsgObj.buildSuccessMsg("初始文件忽略备份");
		} else {
			fileInfoService.create(minemachineId, fileHash, type);
			LOGGER.info("添加文件信息:矿机ID{},文件hash{},类型{}", minemachineId, fileHash, type);
			return MsgObj.buildSuccessMsg();
		}
	}

	/**
	 * 根据矿机ID获取需要备份的hash值
	 * 
	 * @param machineId
	 *            矿机ID
	 * @return
	 */
	@RequestMapping("/getFileHash")
	public MsgObj getFileHash(@NotBlank(message = "矿机ID不能为空!") String minemachineId) {
		LOGGER.info("根据矿机ID获取需要备份的hash值:矿机ID{}", minemachineId);
		List<String> fileHash = fileInfoService.getFileHashByMacId(minemachineId);
		MsgObj msg = MsgObj.buildSuccessObj(fileHash);
		return msg;
	}

	/**
	 * 根据矿机ID获取矿机根目录的hash值
	 * 
	 * @param machineId
	 * @return
	 */
	@RequestMapping("/getRootHash")
	public MsgObj getRootHash(@NotBlank(message = "矿机ID不能为空!") String minemachineId) {
		String rootHash = fileInfoService.getRootHashByMacId(minemachineId);
		MsgObj msg = MsgObj.buildSuccessObj(rootHash);
		return msg;
	}

}
